/* 
 * File:   Socket.h
 * Author: jacek
 *
 * Created on 14 kwiecień 2011, 18:11
 */

#ifndef SOCKET_H
#define	SOCKET_H
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <error.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "SocketException.h"
#include "NetAddress.h"
#include "Package.h"

namespace Net {

class Socket {
	bool listner;
	bool binded;
	bool connected;
	int descryptor;
	sockaddr_in CreateSockAddrIn(char* ip, int port);
public:
	enum Protocol {
		TCP = SOCK_STREAM, UDP = SOCK_DGRAM
	};
	int GetDescryptor() {
		return descryptor;
	}
	void Connect(char* ip, int port);
	void Connect(NetAddress& address);
	Socket();
	Socket(const Socket& src);
	void CreateUDPRecevSocket(NetAddress& address);
	void CreateTCPRecevSocket(NetAddress& address, int qlen);
	void CreateSendingSocket(Protocol protocol);
	void Send(Package& package);
	void SendTo(Package& package, NetAddress& hostAddress);
	void Recev(Package* package);
	void RecevFrom(Package* package,NetAddress* peerAddress);
	void Close();

	static Socket AcceptNewConnetion(Socket* listingSocket,
			NetAddress * peerAddress);

	virtual ~Socket();
private:

};
} // namespace Net

#endif	/* SOCKET_H */

